package com.tanhua.server.service;

import com.tanhua.manage.domain.db.*;
import com.tanhua.manage.domain.vo.PageResult;
import com.tanhua.manage.domain.vo.SettingsVo;
import com.tanhua.manage.domain.vo.UserInfoVoAge;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SettingsService {
    @Reference
    private QuestionApi questionApi;

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private BlackListApi blackListApi;

    @Reference
    private UserInfoApi userInfoApi;

    public SettingsVo querySettings(){
        User user = UserHolder.getUser();
        log.info("user: "+user);
        Long userId = user.getId();

        String strangerQuestion = "你喜欢我吗？";//默认的陌生人问题
        Question question = questionApi.findByUserId(userId);
        if (null != question){
            //用户有设置，使用用户设置的
            strangerQuestion = question.getTxt();
        }
        Settings settings = settingsApi.findByUserId(userId);

        //构建vo
        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setStrangerQuestion(strangerQuestion);
        if (null != settings) {
            //用户有设置，则调用用户设置，否则使用默认true
            BeanUtils.copyProperties(settings, settingsVo);
        }
        settingsVo.setId(userId);
        settingsVo.setPhone(user.getMobile());

        log.info("哈哈，settings"+settings+"settingsVo:"+settingsVo);
        return settingsVo;

    }
    /**
     * 保存通知设置
     * @param vo
     * @return
     */
    public void updateNotification(SettingsVo vo) {
        //获取用户信息
        User user = UserHolder.getUser();
        //读取数据库中用户的设置信息
        Settings settings = new Settings();
        //拷贝vo中的设置到用户设置里
        BeanUtils.copyProperties(vo,settings);
        //设置操作的用户id为登陆用户id
        settings.setUserId(user.getId());
        //将新的settings存入数据库
        settingsApi.save(settings);
    }

    /**
     * 黑名单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult<UserInfoVoAge> blacklist(Long page, Long pageSize) {
        User user = UserHolder.getUser();

        // 1. 调用api通过登陆用户id，分页查询登陆用户的黑名单列表
        PageResult pageResult = blackListApi.findPageByUserId(user.getId(), page, pageSize);

        // 2. 获取分页的结果集，里有黑名单人员的用户id
        List<BlackList> blackLists = pageResult.getItems();
        // 3. 补全黑名单人员的用户详情
        // isEmpty为空
        if (!CollectionUtils.isEmpty(blackLists)){
            //有黑名单
            //批量查询黑名单人员的详情
            List<UserInfoVoAge> voList = new ArrayList<>(blackLists.size());
            for (BlackList blackList : blackLists) {
                // 遍历黑名单人员, 取出黑名单人员的id
                Long blackUserId = blackList.getBlackUserId();
                // 查询这个黑名单人员的信息
                UserInfo blackUserInfo = userInfoApi.findById(blackUserId);
                // 转成vo
                UserInfoVoAge vo = new UserInfoVoAge();
                BeanUtils.copyProperties(blackUserInfo,vo);
                voList.add(vo);

            }
            //重置设置查询的结果
            pageResult.setItems(voList);
        }

        return pageResult;
    }

    /*public PageResult<UserInfoVoAge> blacklist(Long page, Long pageSize) {
        User user = UserHolder.getUser();

        // 1. 调用api通过登陆用户id，分页查询登陆用户的黑名单列表
        PageResult pageResult = blackListApi.findPageByUserId(user.getId(), page, pageSize);

        // 2. 获取分页的结果集，里有黑名单人员的用户id
        List<BlackList> blackLists = pageResult.getItems();
        // 3. 补全黑名单人员的用户详情
        // isEmpty为空
        if (!CollectionUtils.isEmpty(blackLists)){
            //有黑名单
            //批量查询黑名单人员的详情
            List<UserInfoVoAge> voList = new ArrayList<>(blackLists.size());

            *//*for (BlackList blackList : blackLists) {
                // 遍历黑名单人员, 取出黑名单人员的id
                Long blackUserId = blackList.getBlackUserId();
                // 查询这个黑名单人员的信息
                UserInfo blackUserInfo = userInfoApi.findById(blackUserId);
                // 转成vo
                UserInfoVoAge vo = new UserInfoVoAge();
                BeanUtils.copyProperties(blackUserInfo,vo);
                voList.add(vo);

            }*//*
            // 获取所有黑名单人员的id
            List<Long> blackUserIds = blackLists.stream().map(BlackList::getBlackUserId).collect(Collectors.toList());

            //
            for (BlackList blackList : blackLists) {
                // 遍历黑名单人员, 取出黑名单人员的id
                Long blackUserId = blackList.getBlackUserId();
                // 查询这个黑名单人员的信息
                UserInfo blackUserInfo = getUserInfo(blackUserId,userInfoList);
                // 转成vo
                UserInfoVoAge vo = new UserInfoVoAge();
                BeanUtils.copyProperties(blackUserInfo, vo);
                voList.add(vo);
            }
            //重置设置查询的结果
            pageResult.setItems(voList);
        }

        return pageResult;
    }*/

    /**
     * 移除黑名单
     */
    public void removeBlackList(long blackUserId) {
        Long userId = UserHolder.getUserId();
        blackListApi.delete(userId,blackUserId);
    }

    /**
     * 保存陌生人问题
     */
    public void updateQuestion(Map<String, String> paramMap) {

        Question question = new Question();
        question.setTxt(paramMap.get("content"));
        question.setUserId(UserHolder.getUserId());
        questionApi.save(question);

    }

}
